attribute vec4 tangent;
uniform vec3 cameraPosition;

varying vec4 vcol;
varying vec3 lvec,vvec;
varying vec4 T;

vec3 TBN(in vec3 by, in vec4 Tangent)
{
	vec3 binormal 	= cross(gl_Normal.xyz, Tangent.xyz) * Tangent.w;
	vec3 normal		= gl_Normal.xyz; 	

	vec3 tvec;
	tvec.x = dot(Tangent.xyz,  by);
	tvec.y = dot(binormal, by);
	tvec.z = dot(normal,   by);
	return tvec;
}

void main() {
	gl_Position = ftransform();
	
	vec3 light0 = (gl_ModelViewMatrixInverse * gl_LightSource[0].position).xyz - gl_Vertex.xyz;
	light0.y = -light0.y;
	vec3 view = cameraPosition - gl_ModelViewMatrix[3].xyz - gl_Vertex.xyz;

	// DEBUG THESE LINES!!!
	T = tangent; //gl_Color.xyzw;
	
	lvec = TBN(light0, T);
	vvec = TBN(view, T);

	gl_TexCoord[0] = gl_MultiTexCoord0;

	vcol = tangent;
}

